package cn.iocoder.yudao.module.ride.controller.admin.membergrowthrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ride.controller.admin.membergrowthrecord.vo.*;
import cn.iocoder.yudao.module.ride.dal.dataobject.membergrowthrecord.MemberGrowthRecordDO;
import cn.iocoder.yudao.module.ride.service.membergrowthrecord.MemberGrowthRecordService;

@Tag(name = "管理后台 - 会员成长值变动记录")
@RestController
@RequestMapping("/ride/member-growth-record")
@Validated
public class MemberGrowthRecordController {

    @Resource
    private MemberGrowthRecordService memberGrowthRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建会员成长值变动记录")
    @PreAuthorize("@ss.hasPermission('ride:member-growth-record:create')")
    public CommonResult<Long> createMemberGrowthRecord(@Valid @RequestBody MemberGrowthRecordSaveReqVO createReqVO) {
        return success(memberGrowthRecordService.createMemberGrowthRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员成长值变动记录")
    @PreAuthorize("@ss.hasPermission('ride:member-growth-record:update')")
    public CommonResult<Boolean> updateMemberGrowthRecord(@Valid @RequestBody MemberGrowthRecordSaveReqVO updateReqVO) {
        memberGrowthRecordService.updateMemberGrowthRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员成长值变动记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ride:member-growth-record:delete')")
    public CommonResult<Boolean> deleteMemberGrowthRecord(@RequestParam("id") Long id) {
        memberGrowthRecordService.deleteMemberGrowthRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除会员成长值变动记录")
                @PreAuthorize("@ss.hasPermission('ride:member-growth-record:delete')")
    public CommonResult<Boolean> deleteMemberGrowthRecordList(@RequestParam("ids") List<Long> ids) {
        memberGrowthRecordService.deleteMemberGrowthRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员成长值变动记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ride:member-growth-record:query')")
    public CommonResult<MemberGrowthRecordRespVO> getMemberGrowthRecord(@RequestParam("id") Long id) {
        MemberGrowthRecordDO memberGrowthRecord = memberGrowthRecordService.getMemberGrowthRecord(id);
        return success(BeanUtils.toBean(memberGrowthRecord, MemberGrowthRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员成长值变动记录分页")
    @PreAuthorize("@ss.hasPermission('ride:member-growth-record:query')")
    public CommonResult<PageResult<MemberGrowthRecordRespVO>> getMemberGrowthRecordPage(@Valid MemberGrowthRecordPageReqVO pageReqVO) {
        PageResult<MemberGrowthRecordDO> pageResult = memberGrowthRecordService.getMemberGrowthRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemberGrowthRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员成长值变动记录 Excel")
    @PreAuthorize("@ss.hasPermission('ride:member-growth-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMemberGrowthRecordExcel(@Valid MemberGrowthRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemberGrowthRecordDO> list = memberGrowthRecordService.getMemberGrowthRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员成长值变动记录.xls", "数据", MemberGrowthRecordRespVO.class,
                        BeanUtils.toBean(list, MemberGrowthRecordRespVO.class));
    }

}